using System.ComponentModel.Design;
using System.IO;
using System.Windows;
using Honest.MES.Contracts.Interfaces;
using Honest.MES.Contracts.Services;
using Honest.MES.WPF.Constants;
using Honest.MES.WPF.Services;
using Honest.MES.WPF.ViewModels;
using Honest.MES.WPF.Views;
using Prism.Events;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Navigation.Regions;

namespace Honest.MES.WPF
{
    public partial class App
    {
        private ServiceManager _serviceManager;
        private ServiceModuleLoader _serviceModuleLoader;

        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            // ע����־����
            containerRegistry.RegisterSingleton<ILoggerService, NLogLogger>();
            // ע����ķ���
            containerRegistry.RegisterSingleton<IModuleDiscoveryService, ModuleDiscoveryService>();
            containerRegistry.RegisterSingleton<IModuleInitializationService, ModuleInitializationService>();
            containerRegistry.RegisterSingleton<IConfigurationService, ConfigurationService>();
            containerRegistry.RegisterSingleton<IExceptionHandler, ExceptionHandler>();

            // ע��Ӧ�÷���
            containerRegistry.Register<IShellService, ShellService>();

            // ע����ͼģ��
            containerRegistry.Register<MainWindowViewModel>();
            containerRegistry.Register<MenuViewModel>();
            containerRegistry.Register<StatusViewModel>();
            containerRegistry.Register<DefaultViewModel>();

            // ע����ͼ
            containerRegistry.RegisterForNavigation<MenuView>();
            containerRegistry.RegisterForNavigation<StatusView>();
            containerRegistry.RegisterForNavigation<DefaultView>();
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            return new DirectoryModuleCatalog() { ModulePath = Path.Combine(Directory.GetCurrentDirectory(), "Modules") };
        }

        //protected override void OnInitialized()
        protected override async void OnInitialized()
        {
            var eventAggregator = Container.Resolve<IEventAggregator>();
            // 1. 创建服务管理器
            _serviceManager = new ServiceManager(eventAggregator);

            // 2. 创建服务加载器
            _serviceModuleLoader = new ServiceModuleLoader(eventAggregator);

            // 3. 自动扫描并加载 Services/ 目录下的所有服务
            _serviceModuleLoader.LoadServices("Services"); // 相对于 bin/Debug/net48 或 net6.0

            // 4. 将所有自动发现的服务注册到 ServiceManager
            foreach (var service in _serviceModuleLoader.LoadedServices)
            {
                _serviceManager.RegisterService(service);
            }

            // 5. 启动所有服务
            await _serviceManager.StartAllAsync();

            base.OnInitialized();            

            System.Diagnostics.Debug.WriteLine("Ӧ�ó����ʼ�����");

            // ����������Ƿ񴴽�
            if (MainWindow != null)
            {
                System.Diagnostics.Debug.WriteLine($"�������Ѵ���: {MainWindow.Title}");
                MainWindow.Show();
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("������Ϊnull");
            }

            // ��ʼ��ģ��
            var moduleInitialization = Container.Resolve<IModuleInitializationService>();
            moduleInitialization.InitializeModules();

            // ִ����������
            var shellService = Container.Resolve<IShellService>();
            shellService.Initialize();

            // ����ͼע�뵽����
            var regionManager = Container.Resolve<IRegionManager>();
            regionManager.RegisterViewWithRegion(RegionNames.MenuRegion, typeof(MenuView));
            regionManager.RegisterViewWithRegion(RegionNames.StatusRegion, typeof(StatusView));
        }

        protected override async void OnExit(ExitEventArgs e)
        {
            await _serviceManager.ExitAllAsync();
            base.OnExit(e);
        }
    }
}